﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

namespace ProjectSem40701.Admin
{
    public partial class UserRound : System.Web.UI.Page
    {
        /// <summary>
        /// Declare member variables/constances here
        /// </summary>
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        /// <summary>
        /// Sự kiện xảy ra khi page load
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.cbbExam.DataBind();
            }
            EnableDisable_Add_but();
        }

        /// <summary>
        /// Enable or diable the add button
        /// </summary>
        private void EnableDisable_Add_but()
        {
            //neu ko co vong sau hoac ko co thi sinh thi ko cho add
            if (this.cbbNextRound.Items.Count == 0 || this.grvUser.Rows.Count == 0)
            {
                this.btnAddRound.Enabled = false;
            }
            else
            {
                this.btnAddRound.Enabled = true;
            }
        }
        
        /// <summary>
        /// Add thí sinh vào vòng sau
        /// </summary>
        protected void btnAddRound_Click(object sender, EventArgs e)
        {
            try
            {
                int checkboxChecked= 0;
                foreach (GridViewRow row in this.grvUser.Rows)
                {
                    CheckBox checkboxSelected = row.FindControl("chkbSelect") as CheckBox;
                    if (checkboxSelected.Checked)
                    {
                        string strUserID = (row.FindControl("lblUserID") as Label).Text;
                        Guid userID = new Guid(strUserID);
                        AddUserToRound(Convert.ToInt32(this.cbbNextRound.SelectedValue.ToString()), userID);
                        checkboxChecked++;
                    }
                }
                //báo thành công
                if (checkboxChecked > 0)
                {
                    ShowDialog(SUCCESS);
                }
                else 
                {
                    ShowDialog("none");
                }
            }
            catch
            { 
                //báo lỗi
                ShowDialog(FAIL);
            }
        }
        
        private void AddUserToRound(int roundID, Guid userID)
        {
            AdminXSD.UserRoundTableAdapters.tbl_User_RoundTableAdapter dpUserRound
                = new AdminXSD.UserRoundTableAdapters.tbl_User_RoundTableAdapter();
            int ok = dpUserRound.tbl_UserRound_Insert(userID, roundID, 0, Convert.ToDateTime("1/1/1900 12:00:00 AM"), 1);
            if (ok < 1)
            {
                throw new Exception("Thêm mới không thành công");
            }
        }

        /// <summary>
        /// su kien xay ra khi cbbExam duoc thay doi selectedindex
        /// </summary>
        protected void cbbExam_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindRound();
        }

        /// <summary>
        /// su kien xay ra khi cbbExam duoc do day du lieu vao
        /// </summary>
        protected void cbbExam_DataBound(object sender, EventArgs e)
        {
            BindRound();
        }

        /// <summary>
        /// bind du lieu vao cac vong thi theo examID
        /// </summary>
        private void BindRound()
        {
            this.cbbPrevRound.DataBind();
            this.cbbNextRound.DataBind();
            EnableDisable_Add_but();
        }

        //su kien xay ra khi cbbPrevRound duoc thay doi selected index
        protected void cbbPrevRound_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GetMinMark(Convert.ToInt32(this.cbbPrevRound.SelectedValue));
                this.grvUser.DataBind();
                this.cbbNextRound.DataBind();
                EnableDisable_Add_but();
            }
            catch
            { 
                //bao loi
            }
        }

        //lay diem toi thieu cua vong truoc
        private void GetMinMark(int rid)
        {
            AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter dp =
                new AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter();
            DataTable dttblRound = dp.Get_tbl_Round_Select_All_By_RoundID(rid);
            this.lblMinMark.Text = dttblRound.Rows[0]["RoundMinMark"].ToString();
        }
        
        //
        /// <summary>
        ///su kien xay ra khi du lieu duoc do day cbbPrevRound 
        /// </summary>
        protected void cbbPrevRound_DataBound(object sender, EventArgs e)
        {
            GetMinMark(Convert.ToInt32(this.cbbPrevRound.SelectedValue));
            this.grvUser.DataBind();
            EnableDisable_Add_but();
        }

        protected void cbbNextRound_DataBound(object sender, EventArgs e)
        {
            //khong co nguoi dung chon 2 vong giong nhau
            ListItem item = this.cbbPrevRound.SelectedItem;
            this.cbbNextRound.Items.Remove(item);
            this.grvUser.DataBind();
        }

        //chuyen trang
        protected void grvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grvUser.PageIndex = e.NewPageIndex;
            this.grvUser.DataBind();
        }

        //Show dialog here
        private void ShowDialog(string IsSuccess)
        {
            if (IsSuccess == SUCCESS)
            {
                //bao thanh cong
                this.ltrMessage.Text = "Thêm người dùng vào vòng thi thành công";
                this.lblSuccess.Text = SUCCESS;
                this.btnAddRound_ModalPopupExtender.Enabled = true;
                this.btnAddRound_ModalPopupExtender.Show();
            }
            else if(IsSuccess == "none")
            {
                this.ltrMessage.Text = "Không có thí sinh nào được chọn";
                this.lblSuccess.Text = FAIL;
                this.btnAddRound_ModalPopupExtender.Enabled = true;
                this.btnAddRound_ModalPopupExtender.Show();
            }
            else
            {
                this.ltrMessage.Text = "Có lỗi xảy ra, thêm người dùng vào vòng thi không thành công";
                this.lblSuccess.Text = FAIL;
                this.btnAddRound_ModalPopupExtender.Enabled = true;
                this.btnAddRound_ModalPopupExtender.Show();
            }
        }

        //Nút Yes trên modal popup được ấn
        protected void btnYes_Click(object sender, EventArgs e)
        {
            if (this.lblSuccess.Text == SUCCESS)
            {
                Response.Redirect("Exam.aspx");
            }
            else
            {
                this.btnAddRound_ModalPopupExtender.Enabled = false;
            }
        }
    }
}
